package main

import "fmt"

// Author   iGxnon
// CreateAt 2022/2/24

// 两数之合
func main() {
	fmt.Println(twoSum([]int{2, 9, 10, 11}, 20))
}

// 空间复杂度换取时间复杂度，时间复杂度 O(n)
func twoSum(nums []int, target int) []int {
	ans := make([]int, 2)
	set := make(map[int]int)
	for indices, v := range nums {
		set[v] = indices
	}
	for indices, v := range nums {
		find := target - v
		if i, ok := set[find]; ok {
			if indices == i {
				continue
			}
			ans[0] = indices
			ans[1] = i
			return ans
		}
	}
	return nil
}
